package mobidever.godutch.database.sqlitedal;

import java.util.Date;
import java.util.List;

import mobidever.godutch.activity.R;
import mobidever.godutch.database.base.SQLiteDALBase;
import mobidever.godutch.model.ModelUser;
import mobidever.godutch.utility.DateTools;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SQLiteDALUser extends SQLiteDALBase {

	public SQLiteDALUser(Context pContext) {
		super(pContext);
		Log.i("mylog", "SQLiteDALUser over");
	}

	public boolean InsertUser(ModelUser pModelUser) {
		ContentValues _ContentValues = CreateParms(pModelUser);
		long _newID = getDatabase().insert(getTableNameAndPK()[0], null,
				_ContentValues);
		pModelUser.setmUserID((int) _newID);
		return _newID > 0;
	}

	public boolean DeleteUser(String pCondition) {
		return Delete(getTableNameAndPK()[0], pCondition);
	}

	public boolean UpdateUser(String pCondition, ModelUser pModelUser) {
		ContentValues _ContentValues = CreateParms(pModelUser);
		return getDatabase().update(getTableNameAndPK()[0], _ContentValues,
				pCondition, null) > 0;
	}

	public boolean UpdateUser(String pCondition, ContentValues pContentValues) {
		return getDatabase().update("User", pContentValues, pCondition, null)>0;
	}

	public List<ModelUser> getUsers(String pCondition) {
		String _SqlText = "Select * From User Where 1=1 and " + pCondition;
		List<ModelUser> UserList = getList(_SqlText);
		Log.i("mylog", "getList over");
		return UserList;
	}

	@Override
	protected String[] getTableNameAndPK() {

		return new String[] { "User", "UserID" };
	}

	@Override
	protected Object FindModel(Cursor pCursor) {
		ModelUser _ModelUser = new ModelUser();
		_ModelUser.setmUserID(pCursor.getInt(pCursor.getColumnIndex("UserID")));
		_ModelUser.setmUserName(pCursor.getString(pCursor
				.getColumnIndex("UserName")));
		Date _CreateDate = DateTools.getDate(
				pCursor.getString(pCursor.getColumnIndex("CreateDate")),
				"yyyy-MM-dd HH:mm:ss");
		_ModelUser.setmCreateDate(_CreateDate);
		_ModelUser.setmState(pCursor.getInt(pCursor.getColumnIndex("State")));
		return _ModelUser;
	}

	private void InitDefaultData(SQLiteDatabase pDatabase) {
		ModelUser _ModelUser = new ModelUser();
		String _UserName[] = getContext().getResources().getStringArray(
				R.array.InitDefaultUserName);
		for (int i = 0; i < _UserName.length; i++) {
			_ModelUser.setmUserName(_UserName[i]);
			ContentValues _ContentValues = CreateParms(_ModelUser);
			pDatabase.insert(getTableNameAndPK()[0], null, _ContentValues);
		}
	}

	@Override
	public void OnCreate(SQLiteDatabase pDataBase) {
		StringBuilder sCreateTableScript = new StringBuilder();
		sCreateTableScript.append("Create TABLE User(");
		sCreateTableScript
				.append("UserID integer PRIMARY KEY AUTOINCREMENT NOT NULL");
		sCreateTableScript.append(",UserName varchar(10) NOT NULL");
		sCreateTableScript.append(",CreateDate datetime NOT NULL");
		sCreateTableScript.append(",State integer NOT NULL");
		sCreateTableScript.append(")");
		pDataBase.execSQL(sCreateTableScript.toString());

		InitDefaultData(pDataBase);
	}

	@Override
	public void OnUpgrade(SQLiteDatabase pDataBase) {
		// TODO Auto-generated method stub

	}

	public ContentValues CreateParms(ModelUser pInfo) {
		ContentValues _ContentValues = new ContentValues();
		_ContentValues.put("UserName", pInfo.getmUserName());
		_ContentValues.put("CreateDate", DateTools.getFormatDateTime(
				pInfo.getmCreateDate(), "yyyy-MM-dd HH:mm:ss"));
		_ContentValues.put("State", pInfo.getmState());
		return _ContentValues;
	}
}
